package cn.itsource.pethome.org.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.pethome.base.domain.AjaxResult;
import cn.itsource.pethome.base.domain.BaseQuery;
import cn.itsource.pethome.base.domain.PageResult;
import cn.itsource.pethome.base.util.RedisService;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.LoginfoDTO;
import cn.itsource.pethome.org.mapper.DepartmentMapper;
import cn.itsource.pethome.org.mapper.EmployeeMapper;
import cn.itsource.pethome.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    RedisService redisService;

    @Override
    public void insert(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public void delete(Long id) {
        Employee employee = new Employee();
        employee.setDel(Boolean.FALSE);
        employee.setId(id);
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public void deleteBatch(List<Long> ids) throws Exception {
        employeeMapper.DeleteBatch(ids);

    }

    @Override
    public PageResult findPage(BaseQuery query) {
        PageResult<Employee> page = employeeMapper.findPage(query);
        page.getList().forEach(employee -> {
            employee.setDept(departmentMapper.selectByPrimaryKey(Optional.ofNullable(employee.getDepartmentId()).orElse(0L)));
        });
        return page;
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.selectAll();
    }

    /**
     * 后台员工登录的业务逻辑
     * @param employee
     * @return
     */
    @Override
    public AjaxResult login(Employee employee) {
        //1.校验数据,用户名是否存在
        Employee byUsername = employeeMapper.findByUsername(employee.getUsername());
        if (Objects.isNull(byUsername)){
            //1.2用户名不存在,直接抛异常,告诉前台用户名或密码错误
            throw new RuntimeException("用户名或密码错误");
        }else {
            //1.1如果存在,再取出盐,拼接到前台传入的密码中加密,然后与数据库中的密码作对比
            String password = byUsername.getPassword();
            String salt = byUsername.getSalt();
            //将 传入的密码进行加密,与数据的密码做比对
            String digestHex = MD5.create().setSalt(salt.getBytes()).digestHex(employee.getPassword());
            if (!Objects.equals(password,digestHex)){
                throw new RuntimeException("用户名或密码错误");
            }
            //这里就是相等
            //2.已经验证成功,创建uuid
            String uuid = IdUtil.fastSimpleUUID();
            //3.存入redis,key是uuid,value是对象
            redisService.setStringKeyAndValue(uuid,byUsername,30L);
            //4.将key 与value 封装到ajax ,放回给前端
            LoginfoDTO loginfoDTO = new LoginfoDTO(uuid, byUsername);
           return AjaxResult.setResult(loginfoDTO);

        }



    }


    @Override
    public Employee findOne(Long id) {
        return null;
    }
}
